BEGIN {
	FS = "\t|\n|\r|: ";
}

{
	time = substr($1, 1, length($1)-1);	
	eventId = $2;
	dObjId = $3;
	dObjList = $4;
	nodeId = $5;
	nodeList = $6;
	ifaceId = $7;
	divIds = $8 $9;
	eventName = $10; # $9 $10 $11;
	reject = $11;

	if (substr(dObjId,1,1) == " ") {
		dObjId = substr(dObjId,2);
	}
	
	if (reject == "1") {
		dobj_id = dObjId;
		print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dobj_id "','" nodeId "','" ifaceId "','" divIds "','SEND_REJECT');"
	}
	
}

/EVENT_TYPE/ {
	# list of dObj (space separated)
	startPos = 1;

	if (length(dObjId) > 0) {	
		dobj_id = dObjId;
		print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dobj_id "','" nodeId "','" ifaceId "','" divIds "','" eventName "');"
	}

	if (length(dobjList) > 1) {	
		for (pos = startPos; pos < length(dObjList); pos++) {
			if (substr(dObjList, pos, 1) == " ") {
				dobj_id = substr(dObjList, startPos, pos-startPos);
				startPos = pos+1;
				if (dobj_id != "-") {
					print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dobj_id "','" nodeId "','" ifaceId "','" divIds "','" eventName "');"
				}
			}
		}
		
		dobj_id = substr(dObjList, startPos, pos-startPos+1);
		print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dobj_id "','" nodeId "','" ifaceId "','" divIds "','" eventName "');"
	}
}

#/ event/ {
#	if ($3 != "queue" && $4 != "queue") {
#		print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dObjId "','" nodeId "','" ifaceId "','" divIds "','" eventName "');"
#	}
#}

/NodeManager/ {
	print "INSERT INTO events (device, logTime, eventId, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time "," eventId ",'" dObjId "','" nodeId "','" ifaceId "','" divIds "','" eventName "');"
}

/NodeDescription/ {
	dObjId = substr($4,1,40);
	print "UPDATE dataobjects SET type='NodeDescription' WHERE dataobjectId = '" dObjId "';"
}

/Application API/ {
	dObjId = substr($4,1,40);
	print "UPDATE dataobjects SET type='ApplicationAPI' WHERE dataobjectId = '" dObjId "';"
}

/Log started/ {
	print "INSERT INTO events (device, logTime, eventName) VALUES ('" device "'," substr($1, 1, 17) ", 'startHaggle');"
}

/Battery status/ {
	print "INSERT INTO resources (device, logTime, batteryStatus) VALUES ('" device "'," substr($1,1,17) "," $3 ");"
}

/Battery time/ {
	print "INSERT INTO resources (device, logTime, batteryTime) VALUES ('" device "'," substr($1,1,17) "," $3 ");"
}

/Physical memory/ {
	print "INSERT INTO resources (device, logTime, physicalMemory) VALUES ('" device "'," substr($1,1,17) "," $3 ");"
}

/Virtual memory/ {
	print "INSERT INTO resources (device, logTime, virtualMemory) VALUES ('" device "'," substr($1,1,17) "," $3 ");"
}

/kernel event queue/ {
	print "INSERT INTO resources (device, logTime, eventQueueSize) VALUES ('" device "'," substr($1,1,17) "," substr($2,25) ");"
}

#/Event queue length/ {
#	print "INSERT INTO resources (device, logTime, eventQueueLen) VALUES ('" device "'," substr($1,1,17) "," substr($2,20) ");"
#}

/DataStore task queue/ {
	print "INSERT INTO resources (device, logTime, datastoreQueue) VALUES ('" device "'," substr($1,1,17) "," substr($2,29) ");"
}

/ACCEPT/ {
	dobj_id = $3;
	node_id = $4;
	
	print "INSERT INTO events (device, logTime, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time ",'" dobj_id "','" node_id "','-','-','INCOMING_ACCEPT');"
}

/REJECT/ {
	dobj_id = $3;
	node_id = $4;
	
	print "INSERT INTO events (device, logTime, DataobjectId, NodeId, InterfaceId, DivId, eventName) VALUES ('" device "'," time ",'" dobj_id "','" node_id "','-','-','INCOMING_REJECT');"
}
